const { readFileSync } = require('fs');
const { join } = require('path');

const config = readFileSync(
  join(__dirname, 'src', 'common', 'http.interceptor.js'),
).toString();

/**
 * 优先后面寻找，取中间文本
 * @param {string} s 源文本
 * @param {string} start 开始文本
 * @param {string} end 结束文本
 */
const takeMiddleText = (s, start, end) => {
  const beginIndex = s.lastIndexOf(start) + start.length;
  const endIndex = s.indexOf(end, beginIndex);

  return s.slice(beginIndex, endIndex);
};

const target = takeMiddleText(config, "baseUrl: '", "',");

module.exports = {
  devServer: {
    https: false,
    port: 8080,
    proxy: {
      '/api': {
        target,
        changeOrigin: true,
        secure: false,
        pathRewrite: { '^/api': '' },
      },
    },
  },
};
